<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/admin.php';

/**
 * Контроллер просмотра и редактирования персон
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class Admin_Manage_Persons extends Admin {
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return Admin_Manage_Directors
    */
   function Admin_Manage_Persons() {
      parent::Admin();
      $this->set_menu(6, 4);
      $this->load->language('button');
      $this->load->language('video');
   } // end Admin_Manage_Persons
   
   /**
    * Метод по умолчанию
    *
    * @access public
    */
   function index() {
      $data = array();
      $this->load->helper('link');
      $this->load->model('m_select_persons');
      $this->m_select_persons->initialize();
      $this->m_select_persons->add_sort('title');
      $this->load->library('table_builder');
      $this->table_builder->set_model($this->m_select_persons);
      $this->table_builder->add_button(create_link('admin_join_persons'), $this->lang->line('join_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'add'), $this->lang->line('add_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'edit'), $this->lang->line('edit_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'delete'), $this->lang->line('delete_button'), $this->lang->line('persons_delete_confirmation'));
	  
	  $this->table_builder->add_filter('p_dot_title', 'Persons', 'text', 'LIKE');
	  $this->table_builder->add_filter('p_dot_slug', 'URL segment', 'text', 'LIKE');
	  
      $data['table'] = $this->table_builder->generate();
      $this->set_content($this->parser->parse('admin/manage_persons.html', $data, true));
      $this->display();
   } // end index
   
   /**
    * Метод удаления записей
    *
    * @access public
    */
   function delete() {
      $ids = $this->input->post('ids');
      $count = 0;
      if (is_array($ids) && 0 < count($ids)) {
         $this->load->model('m_person');
         foreach ($ids as $id) {
            $this->m_person->set_id($id);
            if ($this->m_person->delete()) {
               $count++;
            }
         }
         if (0 == $count) {
            $this->set_message($this->lang->line('delete_persons_error'), 'error', true);
         } elseif ($count != count($ids)) {
            $this->set_message($this->lang->line('delete_persons_warn'), 'help', true);
         } else {
            $this->set_message($this->lang->line('delete_persons_succ'), 'info', true);
         }
      }
      $this->redirect($this->get_controller());
   } // end delete
   
   /**
    * Метод добавления записи
    * Редирект на нужный контроллер
    *
    * @access public
    */
   function add() {
      $this->redirect('admin_update_person');
   } // end add
   
   /**
    * Метод изменения записи
    * Редирект на нужный контроллер
    *
    * @access public
    */
   function edit() {
      $ids = $this->input->post('ids');
      if (is_array($ids) && 0 < count($ids)) {
         $id = array_shift($ids);
         $this->redirect('admin_update_person', '', array('id' => $id));
      }
      $this->redirect($this->get_controller());
   } // end edit
   
   /**
    * Показ формы выбора персон
    *
    */
   function simple() {
   
      $this->load->helper('link');
      $type = $this->input->get('type');
      $per_page = 20;
      $page = (int) $this->input->get('page');
      if (0 == $page) {
         $page = 1;
      }
      $char = '';
      if (false !== $this->input->get('char')) {
         $char = $this->input->get('char');
      }
      $pagination = '';
      // Подгружаем результаты
      $this->load->model('m_select_persons');
      $this->m_select_persons->set_char($char);
      $this->m_select_persons->initialize();
      $this->m_select_persons->set_limit($per_page, ($page - 1) * $per_page);
      $this->m_select_persons->add_sort('title');
      $this->m_select_persons->load_results();
      $results = $this->m_select_persons->get_results();
      $all_count = $this->m_select_persons->get_all_count();
      
      if ($all_count > count($results)) {
         $this->load->library('pagination');
         $this->pagination->set_page($page);
         $this->pagination->set_num_pages(ceil($all_count / $per_page));
         $this->pagination->set_url(create_link($this->get_controller(), $this->get_method(), array('page' => '{%page%}', 'char' => $char, 'type' => $type)));
         $this->pagination->set_template($this->lang->line('simple_pagination'));
         $pagination = $this->pagination->create_links();
      }
      
      // Ссылки альфапоиска
      $this->load->helper('alphasearch');
      $url = create_link($this->get_controller(), $this->get_method(), array('char' => '{%char%}', 'type' => $type));
      $chars = array();
      $sql = "
         SELECT
            UPPER(SUBSTR(title, 1, 1)) `char`
         FROM
            " . $this->db->dbprefix('persons') . "
         GROUP BY
            `char`
      ";
      $query = $this->db->query($sql);
      if (0 < $query->num_rows()) {
         foreach ($query->result_array() as $result) {
            array_push($chars, $result['char']);
         }
      }
      $d = array(
         'links' => get_alphasearch($url, $chars, 'alpha_en'),
         'char'  => $char
      );
      $alphasearch_en = $this->load->view('en/common/alphasearch', $d, true);
      $d = array(
         'links' => get_alphasearch($url, $chars, 'alpha_ru'),
         'char'  => $char
      );
      $alphasearch_ru = $this->load->view('en/common/alphasearch', $d, true);
      
      $data = array(
         'results'        => $results,
         'pagination'     => $pagination,
         'type'           => $type,
         'alphasearch_en' => $alphasearch_en,
         'alphasearch_ru' => $alphasearch_ru
      );
      
      $this->set_layout('iframe');
      $this->set_content($this->parser->parse('admin/persons_simple.html', $data, true));
      $this->display();
   }
   
}
